package cn.com.gridinfo.sooc.core.my.setting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.my.commen.CodeConst;
import cn.com.gridinfo.sooc.core.my.dao.MyDao;
import cn.com.gridinfo.sooc.core.my.setting.adapter.ProQuestionAdapter;
import cn.com.gridinfo.sooc.core.my.setting.bean.GetQuestion;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 显示所有密保问题列表
 * Created by Administrator on 2015/12/24.
 */
public class QuestionListActivity extends BaseSoocActivity implements AdapterView.OnItemClickListener{
    @InjectView(R.id.question_list)
    ListView questionList;
    ProQuestionAdapter proQuestionAdapter;
    List<GetQuestion> list;
    MyDao myDao;//用户dao
    private String[]  questionIdArray = new String[]{"","",""};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_mibao_question);
        ButterKnife.inject(this);
        myDao=new MyDao(this);
        questionIdArray=getIntent().getStringArrayExtra("questionIdArray");
        myDao.getQuestion();//请求接口
        list=new ArrayList<>();
        proQuestionAdapter=new ProQuestionAdapter(this,list);
        questionList.setAdapter(proQuestionAdapter);
        questionList.setOnItemClickListener(this);

    }
    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "选择密保问题";
    }

    //设置左边按钮
    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //返回
    @OnClick(R.id.toolbar_leftbtn)
    void onGoBack() {
        finish();
    }
    //请求成功
    @Override
    public void onRequestSuccess(int requestCode) {
        super.onRequestSuccess(requestCode);
        if (myDao.status.equals("0")){

        switch (requestCode){
            case CodeConst.GETQUESTION:
                list.addAll(myDao.getQuestions());
                if(null != questionIdArray && questionIdArray.length != 0 )
                    for (int i=0;i<=list.size();i++){

                        if(list.get(i).getId().equals(questionIdArray[0])){
                            list.remove(i);
                        }
                        if(list.get(i).getId().equals(questionIdArray[1])){
                            list.remove(i);
                        }
                        if(list.get(i).getId().equals(questionIdArray[2])){
                            list.remove(i);
                        }
                    }
                proQuestionAdapter.notifyDataSetChanged();
                break;
        }
        }

    }

    @Override
    public void onRequestFaild(int requestCode, String errorNo, String errorMessage) {
        super.onRequestFaild(requestCode, errorNo, errorMessage);
        MessageUtils.showImageShortToast(this,"服务器异常");
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent=new Intent();
        intent.putExtra("proQuestion",list.get(position));
//        intent.putExtra("No",position);
        setResult(RESULT_OK,intent);
        finish();
    }
}
